package com.whmskj.xjlhbc.mobile.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.whmskj.xjlhbc.core.AjaxResult;
import com.whmskj.xjlhbc.core.Page;
import com.whmskj.xjlhbc.domain.PersonnelManagementDataSheets;
import com.whmskj.xjlhbc.domain.Ry1402GroupTable;
import com.whmskj.xjlhbc.dto.PersonnelManagementDto;
import com.whmskj.xjlhbc.dto.QueryDto;
import com.whmskj.xjlhbc.service.IPersonnelManagementDataSheetsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Tag(name = "员工列表",description = "员工列表")
@RequestMapping("/personnel")
@SaIgnore
public class RyController {

    @Autowired
    private IPersonnelManagementDataSheetsService service;

    @GetMapping("/list")
    @Operation(description = "列表")
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class, Ry1402GroupTable.class})
    ))
    @Parameter(name = "dutyType",description = "值班类型")
    public AjaxResult list(@RequestParam String dutyType,String positions1){
        List<Ry1402GroupTable> list = service.queryList(dutyType,positions1);
        return Page.getDataTable(list);
    }

    @GetMapping("/getPostList")
    @Operation(description = "岗位列表")
    @ApiResponse(content = @Content(
            schema = @Schema(anyOf = {AjaxResult.class, List.class})
    ))
    @Parameter(name = "type",description = "值班类型,1:本班组岗位,2:其他岗位")
    public AjaxResult getPostList(@RequestParam(required = false,defaultValue = "1") int type){
        List<Object> list = service.getPostList(type);
        return Page.getDataTable(list);
    }

    /**
     * 分页查询员工列表
     */
    @GetMapping("/getStaffList")
    @Operation(description = "员工列表")
    public AjaxResult getAllEmployees() {
        return service.queryStaffList();
    }

    /**
     * 统计男女人数
     */
    @GetMapping("/getGenderCount")
    @Operation(description = "统计男女人数")
    public AjaxResult getGenderCount(@RequestParam(required = false) String class22,
                                     @RequestParam(required = false) String fe1e,
                                     @RequestParam(required =  false) String sector,
                                     @RequestParam(required =  false) String department){
        Map<String, Integer> list = service.getGenderCount(class22, fe1e, sector,department);
        return AjaxResult.success(list);
    }
    /**
     * 更新员工岗位信息
     * @param dto 请求体中的岗位更新数据
     * @return 统一响应结果
     */
    @PutMapping("/updatePosition")
    public AjaxResult updatePosition(@RequestBody PersonnelManagementDto dto) {
        boolean success = service.updatePositionAndDept(dto);
        if (success) {
            return AjaxResult.success("岗位信息更新成功");
        }
        return AjaxResult.error("更新失败，请检查员工编号是否存在");
    }

    /**
     * 更新员工工作时间
     * @param dto 请求体中的员工工作时间数据
     * @return AjaxResult统一响应
     */
    @PutMapping("/updateWorkingHours")
    public AjaxResult updateWorkingHours(@RequestBody PersonnelManagementDto dto) {
        boolean success = service.updateWorkingHours(dto);
        if (success) {
            return AjaxResult.success("工作时间更新成功");
        }
        return AjaxResult.error("更新失败，请检查员工编号是否存在");
    }

    /**
     * 筛选员工信息
     */
    @GetMapping("/selectby")
    public AjaxResult listPersonnel(@RequestParam(required = false) String class22,
                                    @RequestParam(required = false) String fe1e,
                                    @RequestParam(required =  false) String sector,
                                    @RequestParam(required =  false) String department,
                                    @RequestParam(required =  false) Integer id) {
//            Page.startPage();
            List<PersonnelManagementDto> personnelList = service.getPersonnelByCondition
                                                            (class22, fe1e, sector,department, id);
            return  Page.getDataTable(personnelList);
    }

}
